package com.example.principle.controller;

import com.example.principle.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    private ApplicationContext context;
    @Autowired
    private User singletonUser;

    @Autowired
    private User prototypeUser;

    @Autowired
    private User requestUser;

    @Autowired
    private User sessionUser;

    @Autowired
    private User applicationUser;
    // Bean的作用域

    @RequestMapping("/single")
    public String single() {
        // 从 context 获取对象
        User contextUser = context.getBean("singletonUser", User.class);
        return "contextUser: " + contextUser + " ,<br/> autowiredUser: " + singletonUser;
    }

    @RequestMapping("/prototype")
    public String prototype() {
        // 从 context 获取对象
        User contextUser = context.getBean("prototypeUser", User.class);
        return "contextUser: " + contextUser + " ,<br/> autowiredUser: " + prototypeUser;
    }

    @RequestMapping("/request")
    public String request() {
        // 从 context 获取对象
        User contextUser = context.getBean("requestUser", User.class);
        return "contextUser: " + contextUser + " ,<br/> autowiredUser: " + requestUser;
    }

    @RequestMapping("/session")
    public String session() {
        // 从 context 获取对象
        User contextUser = context.getBean("sessionUser", User.class);
        return "contextUser: " + contextUser + " ,<br/> autowiredUser: " + sessionUser;
    }

    @RequestMapping("/application")
    public String application() {
        // 从 context 获取对象
        User contextUser = context.getBean("applicationUser", User.class);
        return "contextUser: " + contextUser + " ,<br/> autowiredUser: " + singletonUser;
    }

}
